#!/bin/sh
echo "Build started"
cd $(dirname $0)/.. || exit 1
set -e
DOCKER_BUILDKIT=1 docker build -f build-docker/Dockerfile -t ${DOCKER_ENV_CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME} --build-arg branch=${CI_COMMIT_REF_NAME} . || (echo "Build failed" && exit 1)
set +e

echo "Push to Gitlab Registry"
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
docker push ${DOCKER_ENV_CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}
# also push with hash version for history
docker tag ${DOCKER_ENV_CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME} ${DOCKER_ENV_CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}
docker push ${DOCKER_ENV_CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}
[ $? -eq 0 ] && echo "Build done" || (echo "Build failed" && exit 1)
